<!--
 * @Author: cqg
 * @Date: 2020-12-10 09:47:34
 * @LastEditTime: 2021-04-29 18:13:06
 * @Description: file content
 * @LastEditors: cqg
-->

<template>
  <div class="card-box basic-from" :class="{previewPage: type === 'preview'}" v-if="formVisible">
    <el-dialog custom-class="small-padding-dialog" :title="title" :visible.sync="formVisible" width="1200px" @close="close">
      <el-form :model="form" class="form-wrap" ref="form" :rules="rules">
        <p class="title full">合同信息</p>
        <el-form-item label="合同编号" prop="contractNum">
          <el-input v-halfCharCode v-model="form.contractNum" suffix-icon="el-icon-search" :disabled="type !== 'add'" @focus="openChooseDialog()" />
        </el-form-item>
        <el-form-item label="合同类型" prop="contractType">
          <el-select clearable v-model="form.contractType" placeholder="-=请选择=-" disabled filterable>
            <el-option v-for="item in epcContractTypes" :key="item.value" :label="item.label" :value="item.value" />
          </el-select>
        </el-form-item>
        <el-form-item label="合同总金额（元）" prop="contractAmount">
          <el-input :value="form.contractAmount | thousandMoney" disabled />
        </el-form-item>
        <el-form-item label="签订日期" prop="contractDate">
          <el-date-picker v-model="form.contractDate" type="date" placeholder="选择日期" disabled />
        </el-form-item>
        <el-form-item label="乙方单位名称" prop="partybUnit">
          <el-input v-halfCharCode v-model="form.partybUnit" disabled />
        </el-form-item>
        <p class="title full">工程信息</p>
        <el-form-item label="工程名称" prop="projectName">
          <el-input v-halfCharCode v-model="form.projectName" disabled />
        </el-form-item>
        <el-form-item label="工程地址" prop="projectAddress">
          <el-input v-halfCharCode v-model="form.projectAddress" disabled />
        </el-form-item>
        <el-form-item label="工程类型" prop="projectType">
          <el-select clearable v-model="form.projectType" placeholder="-=请选择=-" disabled filterable>
            <el-option v-for="item in epcProjectTypes" :key="item.value" :label="item.label" :value="item.value" />
          </el-select>
        </el-form-item>
        <p class="title full">验收信息</p>
        <el-form-item label="验收日期" prop="acceptDate">
          <el-date-picker v-model="form.acceptDate" value-format="yyyy-MM-dd" type="date" :disabled="type=='preview'" placeholder="选择日期" />
        </el-form-item>
        <el-form-item label="验收结果" prop="acceptResult">
          <el-select clearable v-model="form.acceptResult" placeholder="-=请选择=-" :disabled="type=='preview'">
            <el-option v-for="item in epcAcceptResult" :key="item.value" :label="item.label" :value="item.value" />
          </el-select>
        </el-form-item>
        <el-form-item label="验收意见" :rules=" ['', '20'].includes(form.acceptResult)? rules.remarks: [{ required: false }]" prop="remarks" class="full">
          <el-input v-halfCharCode type="textarea" placeholder="请输入" :disabled="type=='preview'" maxlength="100" v-model="form.remarks" />
        </el-form-item>
      </el-form>
      <p class="title">附件</p>
      <v-upload ref="upload" v-if="formVisible" :busId="busId" modelType="acceptance" @onSuccess="onSuccess" v-model="uploadFileList" :isView="['preview'].includes(type)" />
      <div slot="footer" class="dialog-footer">
        <el-button @click="$bus.$emit('operationLog', form.projectId)" v-if="type === 'preview'">操作日志</el-button>
        <el-button @click="close">取 消</el-button>
        <el-button type="primary" v-throttle="[save]" v-if="['add','edit'].includes(type)">保存</el-button>
      </div>
    </el-dialog>
    <choose-dialog ref="chooseDialog" v-bind="chooseDialogAttrs" @chooseData="handleChoose" :filterItemList="filterItemList" />
  </div>
</template>
<script>
import { updateOrSave } from "./api.js";
import { getDict } from "@/utils";
export default {
  name: "acceptance",
  inject: ["initList"],
  data () {
    return {
      //上传id
      busId: "",
      // 上传列表
      uploadFileList: [],
      //弹框title
      title: "",
      //操作类型
      type: "",
      //验收弹窗开关
      formVisible: false,
      // 表单数据
      form: {
        acceptDate: "",
        acceptResult: "",
        contractAmount: 0,
        contractDate: "",
        contractNum: "",
        contractType: "",
        operateTime: "",
        partybUnit: "",
        projectAcceptId: "",
        projectId: "",
        projectAddress: "",
        projectContractId: "",
        projectName: "",
        projectType: "",
        remarks: ""
      },
      //表单验证规则
      rules: {
        projectName: [
          {
            required: true,
            message: "请填选完整工程信息",
            trigger: "change"
          }
        ],
        contractNum: [
          {
            required: true,
            message: "请填选完整合同信息",
            trigger: "change"
          }
        ],
        acceptDate: [
          {
            required: true,
            message: "请选择验收日期",
            trigger: "change"
          }
        ],
        acceptResult: [
          {
            required: true,
            message: "请选择验收结果",
            trigger: "change"
          }
        ],
        remarks: [
          { required: true, message: "请填写备注", trigger: "blur" },
        ]
      },
    };
  },

  computed: {
    // 工程类型
    epcProjectTypes () {
      return getDict("epc_project_type");
    },
    // 合同类型
    epcContractTypes () {
      return getDict("epc_contract_type");
    },
    //验收结果
    epcAcceptResult () {
      return getDict("epc_accept_result");
    },
    chooseDialogAttrs () {
      return {
        title: "选择合同",
        placeholder: "请输入合同编号",
        url: `/api/epcprojectaccept/selectContract`,
        width: "1200px",
        searchKey: "contractNum",
        showIndex: true,
        showData: [
          {
            prop: "contractNum",
            label: "合同编号",
            width: "200",
            align: "center"
          },
          {
            prop: "contractType",
            label: "合同类型",
            width: "140",
            align: "center",
            formatter: this.dataFormatter
          },
          {
            prop: "contractAmount",
            label: "合同总金额（元）",
            width: "200",
            align: "center"
          },
          {
            prop: "contractDate",
            label: "签订日期",
            width: "200",
            align: "center"
          },
          {
            prop: "partybUnit",
            label: "乙方单位名称",
            width: "200",
            align: "center"
          }
        ],
      }
    },
    filterItemList () {
      return [
        {
          label: "单位名称",
          inputType: "input",
          name: "partybUnit",
        }
      ]
    }
  },
  methods: {
    // 关闭弹窗清空发表单
    close () {
      this.uploadFileList.length = 0;
      for (var i in this.form) {
        this.form[i] = Object.prototype.toString.call(this.form[i]) == "[object Array]" ? [] : "";
      }
      this.formVisible = false;
      this.$refs.form.clearValidate();
    },
    // 监听弹窗选中
    handleChoose (data) {
      this.form = { ...this.form, ...data };
    },
    // 开启选择弹窗
    openChooseDialog () {
      this.$nextTick(() => {
        this.$refs.chooseDialog.show();
      });
    },
    //格式化表格字典值
    dataFormatter (row, column, callValue) {
      let type = "";
      switch (column.property) {
        //合同类型
        case "contractType":
          type = getDict("epc_contract_type").find(
            item => item.value == callValue
          );
          return type ? type.label : "";
      }
    },
    //显示弹窗
    show (type, data = {}) {
      // 判断类型渲染表单标题
      switch (type) {
        case "add":
          this.title = "新增";
          break;
        case "edit":
          this.title = "编辑";
          break;
        case "preview":
          this.title = "查看";
          break;
      }
      this.type = type;
      this.form = { ...this.form, ...data };
      if (data) {
        this.busId = this.form.projectAcceptId;
      }
      this.formVisible = true;
      this.$nextTick(() => {
        this.$refs.form.clearValidate();
      })
    },
    // 保存
    async save () {
      if (["edit", "add"].includes(this.type)) {
        this.$refs.form.validate(async valid => {
          valid && this.$refs.upload.submit();
        });
      }
    },
    // 上传成功
    async onSuccess (response, file, fileList) {
      let params =
        this.type == "add"
          ? { ...this.form, projectAcceptId: response.data }
          : JSON.parse(JSON.stringify(this.form));
      const rps = await updateOrSave(this.type, params);
      if (rps.data.code == 0) {
        this.$message.success(`提交成功!`);
        this.formVisible = false;
        this.initList({ keyword: "", current: 1 });
      } else this.$message.error(rps.data.msg);
    }
  }
};
</script>
<style lang="scss" scoped>
.title {
  border-bottom: 1px solid rgba(97, 106, 120, 0.07);
  height: 50px;
  line-height: 50px;
  font-weight: 700;
  font-size: 16px;
}
</style>
